﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace OpenKey
{
    public partial class KeyDialog : Form
    {
        private List<Flag> _flags = new List<Flag>();
        public KeyInfo Info
        {
            get
            {
                KeyInfo i = new KeyInfo();
                i.Email = textEmail.Text;
                i.Flags = this._flags;
                i.Key = textKey.Text;
                i.KeyFile = textKeyFile.Text;
                i.Name = textName.Text;
                return i;
            }
            set
            {
                textEmail.Text = value.Email;
                this._flags = value.Flags;
                textKey.Text = value.Key;
                textKeyFile.Text = value.KeyFile;
                textName.Text = value.Name;
            }
        }

        public KeyDialog()
        {
            InitializeComponent();
        }

        private void KeyDialog_Load(object sender, EventArgs e)
        {
           
        }

        private void toolGenerateKey_Click(object sender, EventArgs e)
        {
            if (textKey.Text == "")
            {
                string possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                StringBuilder key = new StringBuilder();
                Random rnd = new Random();

                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        key.Append(possible.Substring(rnd.Next(possible.Length), 1));
                    }
                    if (i < 4)
                        key.Append("-");
                }
                textKey.Text = key.ToString();
            }
        }

        private void toolOK_Click(object sender, EventArgs e)
        {
            if (textName.Text != "" && textKey.Text != "")
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Please make sure you have generated a key and input a name to associate with that key.");
            }
        }

        private void toolCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void toolGenerateFile_Click(object sender, EventArgs e)
        {
            if (textName.Text != "" && textKey.Text != "")
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    BinaryWriter w = new BinaryWriter(File.Create("keyfile.tmp"));
                    w.Write("OpenKeyFile");
                    w.Write(_flags.Count);
                    foreach (Flag f in this._flags)
                    {
                        w.Write(f.Name);
                        w.Write(f.Value);
                    }
                    w.Close();
                    string outfile = dlg.SelectedPath + @"\keyfile.elf";
                    Guardian.EncryptFile("keyfile.tmp", outfile, textKey.Text);
                    File.Delete("keyfile.tmp");
                    textKeyFile.Text = outfile;
                }
            }
        }

        private void toolViewInExplorer_Click(object sender, EventArgs e)
        {
            string fileToSelect =textKeyFile.Text;
            string args = string.Format("/Select, {0}", fileToSelect);
            ProcessStartInfo pfi = new ProcessStartInfo("Explorer.exe", args);
            System.Diagnostics.Process.Start(pfi);
        }

        private void toolNew_Click(object sender, EventArgs e)
        {
            FlagDialog fd = new FlagDialog();
            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this._flags.Add(fd.Info);
                listFlags.Items.Add(fd.Info.Name);
            }
        }

        private void toolEdit_Click(object sender, EventArgs e)
        {
            int index = listFlags.SelectedIndex;
            if (index >= 0)
            {
                Flag f = this._flags[index];
                FlagDialog fd = new FlagDialog();
                fd.Info = f;
                if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this._flags[index] = fd.Info;
                    listFlags.DataSource = null;
                    listFlags.DataSource = this._flags;
                }
            }
        }

        private void toolRemove_Click(object sender, EventArgs e)
        {
            int index = listFlags.SelectedIndex;
            if (index >= 0)
            {
                if (MessageBox.Show("Are you sure you wish to remove this flag?", "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    this._flags.RemoveAt(index);
                    listFlags.DataSource = null;
                    listFlags.DataSource = this._flags;
                }
            }
        }
    }
}
